import Vue from 'vue'; 
import VueRouter from 'vue-router';
import routes from './router';
// import FastClick from 'fastclick'; 
// import validatorExtend from 'src/extend/validatorExtend';  
import { Lazyload } from 'mint-ui'; 
import { Loadmore } from 'mint-ui';
import { InfiniteScroll } from 'mint-ui';

Vue.use(InfiniteScroll);
import axios from 'axios'; 
import { axiosPost} from './config';
import Vconsole from 'vconsole';
// import VConsole from 'assets/javascript/vconsole.min.js';

// new VConsole();
// mint-ui轻提示样式
import 'mint-ui/lib/spinner/style.css';
import 'mint-ui/lib/toast/style.css';
import 'mint-ui/lib/indicator/style.css';
import 'mint-ui/lib/message-box/style.css'; 
import store from 'myVuex/store';

import VueExtend from 'src/extend/vueExtend';

import 'assets/style/reset.css';  
import 'assets/style/reset-mint.scss';   
import 'assets/style/x_style.scss';  
import 'assets/style/l_style.scss'; 
import 'assets/style/e_live.scss';
// axios.interceptors.response.use(function (response) {
//     let res = response.data;
//     // console.log(res,555)
//     if (res.code === 403) {
//         window.localStorage.removeItem('userInfo');
//         window.location.href = '/Weixin/Login/index';
//         return;
//     }
//     return response;
// }, function (error) {
//     return Promise.reject(error);
// });

const vConsole = new Vconsole();
// if ('addEventListener' in document) {
//     document.addEventListener('DOMContentLoaded', function() {
//         FastClick.attach(document.body);
//     }, false);
// }
// 注册一个全局自定义指令 `v-focus`
Vue.directive('focus', {
    // 当被绑定的元素插入到 DOM 中时……
    inserted: function (el) {
        // 聚焦元素
        el.focus()
    }
})
  
Vue.use(Lazyload); 
Vue.use(VueRouter);

let router = new VueRouter({
	routes,
	// mode: 'history'
});
 

router.beforeEach((to, from, next) => { 
	// 微信分享
	// if (location.search.indexOf('myshare=') >= 0) {
	// 	var arr = location.search.substr('1').split('&');
	// 	let i = 0;
	// 	let myshare = '';
	// 	for (i; i< arr.length; i++) {
	// 		if (arr[i].indexOf('myshare=') >= 0) {
	// 			myshare = arr[i].split('=')[1];
	// 			// alert(decodeURIComponent(myshare))
	// 			const timestamp = (new Date()).valueOf();
	// 			const random = parseInt(Math.random() * 100000);
	// 			const mytime = timestamp.toString() + random.toString();
	// 			window.location.href = window.location.protocol + '//' + window.location.host + window.location.pathname + '#' + decodeURIComponent(myshare);
	// 		}
	// 	}
	// }
	// 用户授权
// 	if(!to.meta.notRequireAuth) {
//         if (to.meta.title) {
//             document.title = to.meta.title
//         }
// 		// 需要登录
// 		let userInfo = window.localStorage.getItem("userInfo");
// 		if (userInfo && userInfo !== 'undefined') { 
// 			let phone = JSON.parse(userInfo).phone; 
// 			if (!phone && to.fullPath != '/phone?type=1') {
// 				next('/phone?type=1');
// 			} else {
// 				next(); 
// 			}
			
// 		} else { 
// 			axiosPost('/Weixin/Member/getMemberDetail', {}, json => {  
// 				if (json.code == 1) {
// 					window.localStorage.setItem("userInfo", JSON.stringify(json.data));

// 					let phone = json.data.phone;
// 					if (!phone && to.fullPath != '/phone?type=1') {
// 						next('/phone?type=1');
// 					} else {
// 						next(); 
// 					} 
// 				} else { 
// 					window.location.href = '/Weixin/Login/index'; 
// 				}
// 			}); 
// 		}
// 	} else {
// 		next()
// 	}
        if (to.meta.title) {
            document.title = to.meta.title
        }

        // let userInfo = window.localStorage.getItem("userInfo");
        if(to.meta.notRequireAuth){
            next(); 
            return;
        }
     
        axios.post('/Weixin/Member/memberAppeal', {}).then(json => {  
          
            if (json.data.code == 0) {
                router.push('/wait');
            } else { 
               
                axios.post('/Weixin/Member/memberDetail', {}).then(json => { 
                    // console.log(json.data,654654654654654654654654654654654654654654654654654654654654654654654654654);
                    if (json.data.code == 403) {
                        // alert('这是跳转的403验证页面');
                        if(to.name=="Idcard_one"){
                            // alert('这是跳转的身份证后四位验证页面')
                            let orderNumber = encodeURIComponent(to.query.orderNumber);
                            // alert(orderNumber);
                            // let orderNumber = to.query.orderNumber;
                            // window.location.href = `/Weixin/Login/index/${orderNumber}`+'?time='+new Date().getTime();
                            window.location.replace(`/Weixin/Login/index/${orderNumber}`);
                        }else {
                            // alert('这个不是身份证后四位验证页面');
                            // window.location.href = `/Weixin/Login/index`+'?time='+new Date().getTime();
                            window.location.replace(`/Weixin/Login/index`);
                        }
                      
                    } else { 
                        window.localStorage.setItem('userInfo', JSON.stringify(json.data.data));                 
                        if (to.name=="Idcard_one") {
                            console.log(to.query.orderNumber);
                            if (json.data.data.state == 3) {
                                next('/my_order');
                            } else {
                                next();
                            }
                        } else {
                            if(json.data.data.state==1){
                                router.push('/register')
                            }else if(json.data.data.state==2){
                                next()
                            }else if(json.data.data.state==3){
                                next()
                            }
                        }
                    
                    }
                });
            }
        });
            
        
});

  
new Vue({
	router, 
	store,
}).$mount('#app');

